package cn.ytr.crm.web.controller;

import cn.ytr.crm.domain.SystemDictionary;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.ISystemDictionaryService;
import cn.ytr.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/systemDictionary")
@Controller
public class SystemDictionaryController {

	@Autowired
	ISystemDictionaryService systemDictionaryService;

	/**
	 * 显示 字典目录列表页
	 */
	@RequiresPermissions(value = {"访问:字典目录列表页面", "systemDictionary:list"}, logical = Logical.OR)
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		//用分页插件
		model.addAttribute("pageInfo", systemDictionaryService.query(qo));
		return "systemDictionary/list";
	}

	/**
	 * 处理 增加或编辑字典目录页提交的
	 */
	@RequiresPermissions(value = {"操作:新增或修改字典目录", "systemDictionary:saveOrUpdate"}, logical = Logical.OR)
	@RequestMapping("/saveOrUpdate")
    @ResponseBody
	public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {
		JsonResult jsonResult = new JsonResult();
		try {
			systemDictionaryService.saveOrUpate(systemDictionary);
		    return jsonResult;
		} catch (Exception e) {
		    jsonResult.mark("服务器提示：操作字典目录出错");
		    e.printStackTrace();
		    return jsonResult;
		}

	}

	/**
	 * 处理 删除字典目录，需要维护员工表
	 */
    @ResponseBody
    @RequiresPermissions(value = {"操作:删除字典目录", "systemDictionary:delete"}, logical = Logical.OR)
	@RequestMapping("/delete")
	public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
			systemDictionaryService.delete(id);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：删除字典目录操作出错");
            e.printStackTrace();
            return jsonResult;
        }

	}

}
